<?php
namespace app\index\controller;
use app\index\model\UserCookie;
use think\Config;
use app\index\model\User;
class login extends Base
{
    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:首页
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        return view('login/index');
    }

    /**
     * Author:Yang You
     * Time:2018-11-29 15:33:13
     * Describe发送验证码
     * Class Login
     * @package app\admin\controller
     */
    public function sendloginyanma(){
        Config::load(APP_PATH.'admin/config.php');//加载上传配置文件
        $phone=input("phone");
        $ImgApiData = config("ImgApiData");
        $data = ['token' => md5(md5($ImgApiData["key"]).date("Y-m-d")),'mobile' => $phone,'ip' => request()->ip(),];
        $res = $this->http_request($ImgApiData["domain"].'/'.$ImgApiData["ApiList"]["messgeList"], $data);
        $res = json_decode($res);
        if($res->code=="0"){
            session($phone.'yzm',$res->VerificationCode);
        }
        echo $res->code;
    }


    /*
     * 创建人：yangyou
     * 创建时间：2017、8、28
     * 描述：用户手机号登陆
     * */
    public function phonelogin(){
        $phone = input("phone");
        $yanma = input("yanma");
        $yanma2 = session($phone.'yzm');
        if (trim($phone) == "") {
            echo json_encode("请填写您的手机号码");
            exit();
        }
        if (trim($yanma) == "") {
            echo json_encode("请填写您收到的验证码");
            exit();
        }
        if ($yanma2 == null) {
            echo json_encode("您输入的验证码已过期或不存在请重新获取");
            exit();
        }
        if($yanma!=$yanma2){
            echo json_encode("验证码错误，请核对后再试");
            exit();
        }
        $user = new User();
        $UserCookie = new UserCookie();
        $isphone = $user->checkName($phone);//检查是否为新用户
        if ($isphone != null) {
            if ($isphone['user_id']) {
                $cookieInfo = $UserCookie->cookieName(md5(cookie('cookie_name')));
                //如果该cookie没有绑定user_id就将其绑定
                if(empty($cookieInfo["user_id"])){
                    $UserCookie->mySave(["bind_time"=>time(),"user_id"=>$isphone["user_id"]],"key",md5(cookie('cookie_name')));
                }
                $usinfo = array("user_id" => $isphone['user_id'], "tel" => $isphone['tel'], "nick_name" => $isphone['nick_name']);
                session('user_info',$usinfo);
                echo json_encode("ok");
            } else {
                echo json_encode("登陆出错请稍后再试");
            }
        }else{
            //号码不存在创建新用户
            $data = ['tel'=>$phone,'create_ip'=>request()->ip(),'nick_name'=>$phone];
            $username = $user->mySave($data,"user_id",0);
            //将user_id与user_cookie绑定
            $isphone = $user->checkName($phone);
            $UserCookie->mySave(["bind_time"=>time(),"user_id"=>$isphone["user_id"]],"key",md5(cookie('cookie_name')));
            if ($username) {
                session('user_info',$data);
                echo json_encode('ok2');
            } else {
                echo json_encode("登陆出错请稍后再试");
            }
        }

    }

}